<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>09-光照</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    html,
    body {
      width: 100%;
      height: 100%;
      overflow: hidden;
    }
  </style>
</head>

<body>
  <script type="module">
    // 引入three.js
      import * as THREE from "three";
      import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";

      //创建场景
      const scene = new THREE.Scene();

      //创建相机
      const camera = new THREE.PerspectiveCamera(
        45,
        window.innerWidth / window.innerHeight,
        0.1,
        1000
      );
      //设置相机位置
      camera.position.set(0,15, 10); //统一设置
      //设置相机朝向点位
      camera.lookAt(0, 0, 0);

      //创建渲染器
      const renderer = new THREE.WebGLRenderer({
        antialias: true, //减少边缘齿轮
      });
      renderer.setSize(window.innerWidth, window.innerHeight);
      document.body.appendChild(renderer.domElement);

      //光源
      const ambientLight = new THREE.AmbientLight( 0xff0000,1 ); // 环境光
      // scene.add(ambientLight)

      const directionalLight = new THREE.DirectionalLight( 0xffffff, 0.5 );//平行光
      directionalLight.position.set(5,5,0) //光源位置
      // scene.add(directionalLight)

      const directionalLightHelper = new THREE.DirectionalLightHelper( //平行光辅助对象
        directionalLight
      )
      // scene.add(directionalLightHelper)

      const hemisphereLight = new THREE.HemisphereLight(0xff0000,0x0000ff,1) //半球光
      // scene.add(hemisphereLight)

      const pointLight = new THREE.PointLight(0xff0000,1,100) //点光源 （颜色 光照强度 照射最大距离 衰退量）
      pointLight.position.set(0,5,0)
      scene.add(pointLight)

      const pointLightHelper = new THREE.PointLightHelper( pointLight, 1 ); //点光源辅助对象
      scene.add( pointLightHelper );

      //创建结构-几何体
      const plane = new THREE.Mesh(
        new THREE.PlaneGeometry(10,10),
        new THREE.MeshStandardMaterial()
      )
      plane.rotation.x = -Math.PI / 2

      const cube = new THREE.Mesh(
        new THREE.BoxGeometry(2,2,2),
        new THREE.MeshStandardMaterial()
      )
      cube.position.set(0,2,0)

      const sphere = new THREE.Mesh(
        new THREE.SphereGeometry(1,32,32),
        new THREE.MeshStandardMaterial()
      )
      sphere.position.set(3,2,0)

      scene.add(plane,cube,sphere); //在场景中添加物体

      //控制插件
      const controls = new OrbitControls(camera, renderer.domElement);
      controls.enableDamping = true //开启惯性,需要和update方法配合使用
      controls.addEventListener("change", () => {
        //渲染
        renderer.render(scene, camera);
      });


      //动画函数
      function animate() {
        requestAnimationFrame(animate);
        controls.update()
        renderer.render(scene, camera);
      }
      animate();
    </script>
</body>

</html>